<?php

// ------------------------------------------------------------------------
// |@Author       : Jarmin <edshop@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2025-05-07 07:40:35
// |@----------------------------------------------------------------------
// |@LastEditTime : 2025-05-11 01:44:32
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Description  :
// |@----------------------------------------------------------------------
// |@FilePath     : SystemApiGrayReleases.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2025 http://www.ladmin.cn All Rights Reserved.
// ------------------------------------------------------------------------
declare (strict_types=1);

namespace think\admin\model;

use think\admin\Model;

/**
 * 系统API灰度发布模型
 * Class SystemApiGrayReleases
 * @package think\admin\model
 */
class SystemApiGrayReleases extends Model
{
    // 设置字段信息
    protected $schema = [
        'id'               => 'int',
        'version'          => 'string',
        'status'           => 'int',
        'fallback_version' => 'string',
        'gray_percent'     => 'int',
        'white_list'       => 'string',
        'start_time'       => 'datetime',
        'end_time'         => 'datetime',
        'description'      => 'string',
        'created_at'       => 'datetime',
        'updated_at'       => 'datetime',
    ];

    // 获取器
    public function getStatusTextAttr($value, $data)
    {
        $status = [0 => '禁用', 1 => '启用'];
        return $status[$data['status']] ?? '未知';
    }

    public function getWhiteListAttr($value)
    {
        return $value ? explode(',', $value) : [];
    }

    // 修改器
    public function setVersionAttr($value)
    {
        return strtolower($value);
    }

    public function setFallbackVersionAttr($value)
    {
        return strtolower($value);
    }

    public function setWhiteListAttr($value)
    {
        if (is_array($value)) {
            return implode(',', $value);
        }
        return $value;
    }

    // 关联版本
    public function version()
    {
        return $this->belongsTo(ApiVersion::class, 'version', 'version');
    }

    // 关联回退版本
    public function fallbackVersion()
    {
        return $this->belongsTo(ApiVersion::class, 'fallback_version', 'version');
    }
}
